Ext.define('StaffServer.controller.TrainInfoController', {
	extend : 'Ext.app.Controller',

	models : ['TeacherModel', 'TrainInfoModel'],
	stores : ['TeacherStore', 'TrainInfoStore'],
	views : ['traininfo.TrainInfoMngr',
			'traininfo.TrainInfoSearchPanel', 'traininfo.TrainInfoDetailGrid',
			'traininfo.TrainInfoDetailWindow',
			'traininfo.TrainInfoDetailForm', 'traininfo.TrainInfoExcelWindow',
			'traininfo.TrainInfoExcelForm'],

	tabView : 'StaffServer.view.traininfo.TrainInfoMngr',
	trainInfoDetailWindow : null,
	trainInfoDetailForm : null,
	
	trainInfoForm : null,
	trainInfoExcelWindow : null,
	trainInfoExcelForm : null,
	submitFlag : null,

	staffRecord : null,
	refs : [{
				ref : 'trainInfoSearchPanel',
				selector : 'trainInfoSearchPanel'
			}, {
				ref : 'trainInfoDetailGrid',
				selector : 'trainInfoDetailGrid'
			}],

	// 初始化执行语句
	init : function() {
		
		trainInfoDetailWindow = Ext.widget("trainInfoDetailWindow");
		trainInfoDetailForm = Ext.ComponentQuery.query('trainInfoDetailForm')[0];
		trainInfoExcelWindow = Ext.widget("trainInfoExcelWindow");
		trainInfoExcelForm = Ext.ComponentQuery.query('trainInfoExcelForm')[0];

		// 控制事件
		this.control({
					'trainInfoSearchPanel button[action=search]' : {
						click : this.search
					},
					'trainInfoSearchPanel button[action=reset]' : {
						click : this.reset
					},
					
					'trainInfoDetailGrid button[action=saveTrainInfo]' : {
						click : this.saveTrainInfo
					},
					'trainInfoDetailWindow button[action=close]' : {
						click : this.closeDetailWindow
					},
					'trainInfoDetailWindow button[action=submit]' : {
						click : this.submitDetialWindow
					},
					'trainInfoDetailGrid button[action=deleteTrainInfo]' : {
						click : this.deleteTrainInfo
					},
					'trainInfoDetailGrid button[action=updateTrainInfo]' : {
						click : this.updateTrainInfo
					},
					
					'trainInfoDetailGrid button[action=exportExcel]' : {
						click : this.exportExcel
					},
					'trainInfoDetailGrid button[action=importExcel]' : {
						click : this.importExcel
					},
					'trainInfoExcelWindow button[action=submit]' : {
						click : this.submitExcelWindow
					},
					'trainInfoExcelWindow button[action=close]' : {
						click : this.closeExcelWindow
					}
				});
	},

	// 提交表单
	submitExcelWindow : function() {
		if (!trainInfoExcelForm.form.isValid()) {
			return;
		}
		var $this = this;
		var values = trainInfoExcelForm.getValues();
		trainInfoExcelForm.getForm().submit({
					waitTitle : '系统提示',
					waitMsg : '正在处理,请稍候',
					clientValidation : true,
					url : 'traininfo/importExcel',
					params : {
						values : values
					},
					success : function(form, action) {
						Ext.Msg.alert('提示', "操作成功", function() {
									$this.closeExcelWindow();
									$this.search();
								});
					},
					failure : function(form, action) {
					var resp = Ext.JSON.decode(response.responseText);
                        Ext.Msg.alert('提示', resp.error);
					}
				});
	},
	// 关闭窗口
	closeExcelWindow : function() {
		trainInfoExcelForm.getForm().reset();
		trainInfoExcelWindow.hide();
	},

	// 导入
	importExcel : function() {
		trainInfoExcelWindow.setTitle("导入文件");
		trainInfoExcelWindow.show();
	},

	exportExcel : function() {
		var searchPanel = this.getTrainInfoSearchPanel();
		var searchParams = searchPanel.getForm().getFieldValues();
		var jsonFilter = Ext.JSON.encode(searchParams);
		window.location.href = 'traininfo/exportTrainInfoExcel?jsonFilter='
				+ jsonFilter;
	},
	

	// 更新
	updateTrainInfo : function() {
		var record = this.getTrainInfoDetailGrid().getSelectionModel()
				.getLastSelected();
		if (Ext.isEmpty(record)) {
			Ext.Msg.alert('提示', '请先选择要操作的记录!');
			return;
		}
		trainInfoDetailForm.loadRecord(record);
		submitFlag = "update";
		trainInfoDetailWindow.setTitle("修改员工培训信息");
		trainInfoDetailWindow.show();
	},

	closeDetailWindow : function() {
		trainInfoDetailForm.getForm().reset();
		trainInfoDetailWindow.hide();

	},

	// 删除
	deleteTrainInfo : function() {
		var $this = this;
		var record = this.getTrainInfoDetailGrid().getSelectionModel()
				.getLastSelected();
		if (Ext.isEmpty(record)) {
			Ext.Msg.alert('提示', '请先选择要操作的记录!');
			return;
		}
		Ext.MessageBox.confirm('请确认', '确认删除吗?', function(btn, text) {
					if (btn == 'yes') {
						showWaitMsg();
						Ext.Ajax.request({
									url : 'traininfo/deleteTrainInfo',
									params : {
										sn : record.get("sn")
									},
									success : function(response) {
										var resp = Ext.JSON
												.decode(response.responseText);
										if (resp.success) {
											Ext.Msg.alert('提示', '操作成功',
													function() {
														$this
																.getTrainInfoDetailGrid()
																.getStore()
																.reload();
													});
										} else {
											Ext.Msg.alert('提示', resp.error);
										}
									},
									failure : function(response) {
										var resp = Ext.JSON
												.decode(response.responseText);
										Ext.Msg.alert('提示', resp.error);
									}
								});
					}
				});
	},

	// 提交表单
	submitDetialWindow : function() {
		if (!trainInfoDetailForm.form.isValid()) {
			return;
		}
		var $this = this;
		var values = trainInfoDetailForm.getValues();
		trainInfoDetailForm.getForm().submit({
			waitTitle : '系统提示',
			waitMsg : '正在处理,请稍候',
			clientValidation : true,
			url : submitFlag == 'save'
					? 'traininfo/saveTrainInfo'
					: 'traininfo/updateTrainInfo',
			// url: submitFlag == 'save' ? 'degreeStudy/saveDegreeStudy' :
			// 'degreeStudy/updateDegreeStudy',
			params : {
				values : values
			},
			success : function(form, action) {
				Ext.Msg.alert('提示', "操作成功", function() {
							$this.closeDetailWindow();
							$this.getTrainInfoDetailGrid().getStore().reload();
						});
			},
			failure : function(form, action) {
				var resp = Ext.JSON.decode(action.response.responseText);
				Ext.Msg.alert('提示', resp.error);
			}
		});
	},

	// 新增
	saveTrainInfo : function() {
		submitFlag = "save";
		trainInfoDetailWindow.setTitle("新增员工培训信息");
		trainInfoDetailWindow.show();
	
	},
	

	// searchPanel查询
	search : function() {
		var searchPanel = this.getTrainInfoSearchPanel();
		var grid = this.getTrainInfoDetailGrid();
		grid.store.on('beforeload', function(store) {
					var searchParams = searchPanel.getForm().getFieldValues();
					Ext.apply(store.proxy.extraParams, searchParams);
				});
		Ext.ComponentQuery.query('trainInfoDetailGrid > pagingtoolbar')[0]
				.moveFirst();
	},

	// searchPanel重置
	reset : function() {
		var searchPanel = this.getTrainInfoSearchPanel();
		searchPanel.getForm().reset();
	}

});